

project , uses(../data_output/legislators_klarner.dta)

	
	***********
	** Nevada
	** Life-time term limites: 6 x 2-year terms.
	** First year: 2010
	***********	
	use ../data_output/legislators_klarner.dta, clear
	keep if state=="NV" & chamber=="house"
	keep if regexm(term, "1999|2001|2003|2005|2007|2009|2011|2013|2015")
	gen lastname = trim(regexs(1)) if regexm(lower(CandName),"([^,]+)")
	gen firstname = trim(regexs(1)) if regexm(lower(CandName),",( [^ ]+)")
	gen mergename = lastname
	bys chamber mergename term: gen n = _n
	by chamber mergename term: egen mean_n=mean(n)
	replace mergename = CandName if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n
	save ../data_output/legislators_NV_house_1999_2016.dta, replace
	
	gen e_won = elections_won
	qui forval i= 1960(2)1996{
		replace e_won = subinstr(e_won, "`i';", "",.)
	}
	tostring year, gen(regexp)
	replace regexp = "^([^!]+"+regexp+";)"
	gen previous_wins = regexs(1) if regexm(e_won, regexp)
	egen prev_terms = noccur(previous_wins) , string(";")
	gen termlimited=prev_terms==6
	save ../data_output/legislators_NV_house_1999_2016.dta, replace
project, creates(../data_output/legislators_NV_house_1999_2016.dta)
	

	
project , original(../data_input/NV/nv_bills_78th2015.csv)	
	import delimited using ../data_input/NV/nv_bills_78th2015.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)

	clear
	gen name = ""
	tempfile tmp
	save `tmp', replace
	
	foreach file in nv_bills_70th1999 nv_bills_71st2001 nv_bills_72nd2003 nv_bills_73rd2005 nv_bills_74th2007 nv_bills_75th2009 nv_bills_76th2011 nv_bills_77th2013 nv_bills_78th2015 nv_bills_17th2001Special nv_bills_18th2002Special nv_bills_19th2003Special nv_bills_20th2003Special nv_bills_21st2004Special nv_bills_22nd2005Special nv_bills_23rd2007Special nv_bills_24th2008Special nv_bills_25th2008Special nv_bills_27th2013Special nv_bills_28th2014Special nv_bills_29th2015Special nv_bills_30th2016Special {
project, original(../data_input/NV/`file'.csv)	
		import delimited ~/Dropbox/web_scrape/bills/nv/`file'.csv , delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1) clear   stringcols(_all)  
		append using `tmp'
		save `tmp', replace
	}

	
	drop if name==""
	gen term = ""
	replace term = "1985-1986" if regexm(session, "1985|1986")
	replace term = "1987-1988" if regexm(session, "1987|1988")
	replace term = "1989-1990" if regexm(session, "1989|1990")
	replace term = "1991-1992" if regexm(session, "1991|1992")
	replace term = "1993-1994" if regexm(session, "1993|1994")
	replace term = "1995-1996" if regexm(session, "1995|1996")	
	replace term = "1997-1998" if regexm(session, "1997|1998")
	replace term = "1999-2000" if regexm(session, "1999|2000")
	replace term = "2001-2002" if regexm(session, "2001|2002")
	replace term = "2003-2004" if regexm(session, "2003|2004")
	replace term = "2005-2006" if regexm(session, "2005|2006")
	replace term = "2007-2008" if regexm(session, "2007|2008")
	replace term = "2009-2010" if regexm(session, "2009|2010")
	replace term = "2011-2012" if regexm(session, "2011|2012")
	replace term = "2013-2014" if regexm(session, "2013|2014")
	replace term = "2015-2016" if regexm(session, "2015|2016")
	gen mergename = lower(name) 
	replace mergename = subinstr(mergename, "-","",.)
	replace mergename = subinstr(mergename, "'","",.)
	
	replace mergename = "ANDERSON, ELLIOT T." if regexm(mergename,"elliot anderson")==1 & regexm(term,"2013|2015")
	replace mergename = "ANDERSON, PAUL" if regexm(mergename,"paul anderson")==1 & regexm(term,"2013|2015")	
	replace mergename = "arberry" if regexm(mergename,"arberry jr.")==1 & regexm(term,"1999|2001|2003|2005")
	replace mergename = "donderoloop" if regexm(mergename,"dondero")==1 & regexm(term,"2009|2011|2013")
	replace mergename = "bustamanteadams" if regexm(mergename,"bustamante adams")==1 & regexm(term,"2011|2013|2015")
	replace mergename = "debraga" if regexm(mergename,"de braga")==1 & regexm(term,"1999|2001")
	replace mergename = "vontobel" if regexm(mergename,"von tobel")==1 & regexm(term,"1999|2001")
	
	**fiscal effect
	replace fiscal_impact = regexs(1) if regexm(fiscal_impact, "^([ ,.$()0-9]+);")
	replace fiscal_impact  = subinstr(fiscal_impact, ";"," ",.)
	replace fiscal_impact  = subinstr(fiscal_impact, "$"," ",.)
	replace fiscal_impact = trim(regexr(fiscal_impact, "\s\s+"," "))
	qui split fiscal_impact, generate(effect) parse(" ")
	qui foreach v of varlist effect* {
		replace `v' = "-" + `v' if regexm(`v', "[()]")
		replace `v' = subinstr(`v', "(", "", .)
		replace `v' = subinstr(`v', ")", "", .)
	}
	qui destring effect*, replace force
	replace effect1 = 0 if effect1==.
	egen fiscal_effect = rowmean(effect*) ,
	drop effect*
	
	
	merge m:1  mergename term  using ../data_output/legislators_NV_house_1999_2016.dta
	*3 obs do not merge
	drop if _merge==1
	
	gen billlength = length(billtext)
	gen law = 0
	replace law=1 if regexm(lower(history), "approved by the governor|enrolled")
	gen sponsor_approp = regexm(lower(history), "ways [&] means|ways and means")
	save ../data_output/bills_by_sponsor_NV_1999_2016.dta, replace
project, creates(../data_output/bills_by_sponsor_NV_1999_2016.dta)
	
	
	use ../data_output/bills_by_sponsor_nv_1999_2016.dta, clear
	gen sponsor = 1 if bill!=""
	gen fiscal_analysis = fiscal_link!=""
	gen fiscal_positive = fiscal_effect>0
	gen fiscal_negative = fiscal_effect<0
	gen fiscal_neutral = fiscal_effect==0	
	gen law_fiscal_effect = 0 if fiscal_effect!=.
	replace law_fiscal_effect = fiscal_effect if law==1

	
	collapse (sum) fiscal_analysis fiscal_positive fiscal_negative fiscal_neutral sponsor* billlength law total_fiscal_effect = fiscal_effect (mean) law_fiscal_effect mean_fiscal_effect = fiscal_effect (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party mergename inoffice year termlimited vtsh, by(CandId term )

	foreach v of varlist sponsor_approp billlength law *fiscal* {
	bys  term: egen max = max(`v')
		replace `v' = . if max==0
		drop max
	}	
	
	save ../data_output/total_bills_by_sponsor_NV_1999_2016.dta, replace
project, creates(../data_output/total_bills_by_sponsor_NV_1999_2016.dta)

	
	
	**voting in NV
	use ../data_output/legislators_klarner.dta, clear
	gen lastname = regexs(1) if regexm(lower(CandName),"([^,]+), [^!]")
	gen firstname = regexs(1) if regexm(lower(CandName),"[^,]+, ([^!])")
	keep if state=="NV" & chamber=="house"
	keep if regexm(term, "1999|2001|2003|2005|2007|2009|2011|2013|2015")
	gen os_mergename = lastname
	bys  os_mergename term: gen n = _n
	by  os_mergename term: egen mean_n=mean(n)
	replace os_mergename = firstname +" " + lastname    if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n
	tempfile tmp
	save `tmp', replace
	
project, original(../data_input/NV/nv_votes.csv)	
	import delimited using ../data_input/NV/nv_votes.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	replace chamber = "house" if chamber=="assembly"
	keep if chamber=="house"
	tostring session, replace
	gen term = ""
	replace term = "1999-2000" if regexm(session, "1999|2000")
	replace term = "2001-2002" if regexm(session, "2001|2002")
	replace term = "2003-2004" if regexm(session, "2003|2004")
	replace term = "2005-2006" if regexm(session, "2005|2006")
	replace term = "2007-2008" if regexm(session, "2007|2008")
	replace term = "2009-2010" if regexm(session, "2009|2010")
	replace term = "2011-2012" if regexm(session, "2011|2002")
	replace term = "2013-2014" if regexm(session, "2013|2014")
	replace term = "2015-2016" if regexm(session, "2015|2016")
	gen NAME = trim( subinstr(lower(name), "jr.", "", .) )
	replace NAME = subinstr(NAME, ",","",.)
	replace NAME = regexs(1) if regexm(lower(NAME), "([^ ]+)$")
	gen os_mergename = trim(lower(NAME))
	replace os_mergename = lower(os_mergename)
	replace os_mergename = subinstr(os_mergename,"’","",.)
	replace os_mergename = subinstr(os_mergename,"'","",.)
	replace os_mergename = subinstr(os_mergename,"-","",.)	
	replace os_mergename = "debraga"	if os_mergename=="braga" & regexm(term, "1999|2001")
	replace os_mergename = "vontobel"	if os_mergename=="tobel" & regexm(term, "1999|2001")
	replace os_mergename = "donderoloop"	if os_mergename=="loop" & regexm(term, "2009|2011|2013")
	replace os_mergename = "bustamanteadams"	if os_mergename=="adams" & regexm(term, "2011|2013|2015")
	replace os_mergename = "e anderson"	if name=="Elliot Anderson" & regexm(term, "2013|2015")
	replace os_mergename = "p anderson"	if name=="Paul Anderson" & regexm(term, "2013|2015")
		
	merge m:1  os_mergename term using `tmp'		

	// 3 obs not merging
	drop if _merge==1
	drop _merge	
	replace vote = lower(vote)
	gen floorvotes = regexm(vote, "yea|nay")
	replace vote = "1" if vote=="yea"
	replace vote = "-1" if vote=="nay"
	replace vote = "0" if regexm(vote, "1")==0
	destring vote, replace
	egen vote_id = group(session	chamber	location	bill	date url), missing
	bys party vote_id : egen party_mean=mean(vote)
	gen party_vote = 0
	replace party_vote = 1 if party_mean>0
	replace party_vote = -1 if party_mean<0
	gen vote_party_line = vote==party_vote
	gen vote_against_party = vote==-party_vote
	bys vote_id: egen votemargin = mean(vote)
	gen close_floorvotes = floorvotes if abs(votemargin)<.1	
	
	save ../data_output/floorvotes_NV_1999_2016.dta, replace
project, creates(../data_output/floorvotes_NV_1999_2016.dta)
	
	use ../data_output/floorvotes_NV_1999_2016.dta, clear
	collapse  (mean) vote_party_line vote_against_party (sum) *floorvotes (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party   year  vtsh, by(CandId term )
	
	save ../data_output/total_floorvotes_NV_1999_2016.dta, replace
project, creates(../data_output/total_floorvotes_NV_1999_2016.dta)	
	
